﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinCourseControls
{
    public partial class FrmTimer : Form
    {
        public FrmTimer()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 指定间隔后引发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        
        int i = 1;

        /// <summary>
        /// 用于判断图片1 向左移动 0向右移动
        /// </summary>
        int p = 1;
        Point start = new Point();
        private void timer1_Tick(object sender, EventArgs e)
        {
            //主线程执行   
            lblCurTime.Text= DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
           
            // Thread.Sleep(2000);//等待时间    影响下次触发
        }

        private void FrmTimer_Load(object sender, EventArgs e)
        {
            // timer1.Enabled = true;//
            lblCurTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            start = pic.Location;
            timer1.Start();
            timer2.Start();
        }

        /// <summary>
        /// 暂停按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPause_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled)
                timer1.Stop();
            else
                timer1.Start();
        }

        /// <summary>
        /// 图片移动定时事件 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (p == 1 && i > 50)
                p = 0;
            else if (p == 0 && i == 1)
                p = 1;
            if (i <= 50 && p == 1)
            {
                pic.Location = new Point(pic.Location.X + 5, pic.Location.Y);
                i++;
            }
            else if (i >= 1 && p == 0)
            {
                // 设置图片移动距离
                pic.Location = new Point(pic.Location.X - 5, pic.Location.Y);
                i--;
            }
            else if (i == 1)
            {
                pic.Location = start;
            }
        }
    }
}
